package com.chinaisafe.user.service.impl;

import com.chinaisafe.user.entity.SysDept;
import com.chinaisafe.user.mapper.SysDeptMapper;
import com.chinaisafe.user.service.SysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.user.vo.request.SysDeptReqVO;
import com.chinaisafe.user.vo.response.SysDeptRespVO;
import org.springframework.stereotype.Service;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.constant.MybatisplusConstants;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;

import java.util.Arrays;
import java.util.Optional;
/**
 * SysDeptServiceImpl
 * Description：部门服务实现类
 *
 * @author xuefu.shu
 * @date 2024-07-12 10:39:32
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Override
    public IPage<SysDeptRespVO> getPageList(SysDeptReqVO sysDeptReqVO) {
        // 分页设置
        Page<SysDept> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysDeptReqVO);
        // 查询条件构造
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDept::getDeleted, MybatisplusConstants.LOGIC_NOT_DELETE_VALUE);
        return baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDeptRespVO add(SysDeptReqVO sysDeptReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysDeptReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDeptRespVO update(SysDeptReqVO sysDeptReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysDeptReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDeptRespVO addOrUpdate(SysDeptReqVO sysDeptReqVO) {
        Long sysDeptId = sysDeptReqVO.getId();
        if (sysDeptId != null){
            // 编辑
            SysDept sysDept = baseMapper.selectById(sysDeptId);
            Optional.ofNullable(sysDept).orElseThrow(() -> BizException.build("未找到要修改的部门信息"));
        }
        SysDept sysDept2Save = sysDeptReqVO.convertToEntity();
        boolean success = this.saveOrUpdate(sysDept2Save);
        if (success) {
            return SysDeptRespVO.convertFromEntity(sysDept2Save);
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public SysDeptRespVO getDetailById(Long sysDeptId) {
        SysDept sysDept = baseMapper.selectById(sysDeptId);
        Optional.ofNullable(sysDept).orElseThrow(() -> BizException.build("未找到对应部门信息"));
        return SysDeptRespVO.convertFromEntity(sysDept);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] sysDeptIds) {
        if (sysDeptIds.length > 0) {
            this.removeByIds(Arrays.asList(sysDeptIds));
        }
    }
}
